package com.akk.platform;

import com.akk.module.app.service.AppUserGradeAccessService;
import com.akk.module.avs.entity.AvsAccess;
import com.akk.module.avs.model.request.GradeAccessReq;
import com.akk.module.avs.model.response.GradeAccessRes;
import com.akk.module.avs.service.AvsAccessService;
import com.akk.utils.IDUtils;
import com.akk.utils.R;
import com.akk.utils.TablePage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/platform/access")
public class AccessAction {

    @Autowired
    private AvsAccessService accessService;

    @Autowired
    private AppUserGradeAccessService appUserGradeAccessService;

    @RequestMapping("/list")
    public TablePage list(TablePage tablePage) {
        Map param = tablePage.getParam();
        PageHelper.startPage(tablePage.getPageNumber(),tablePage.getPageSize());
        List<AvsAccess> accesss=accessService.findByParam(param);
        tablePage.setRows(accesss);
        tablePage.setTotal(new PageInfo<AvsAccess>().getTotal());
        return tablePage;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public R add (@RequestBody AvsAccess access){
        access.setAccessId(IDUtils.getPK());
        accessService.saveAccess(access);
        return R.ok();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public R update(@RequestBody AvsAccess access){
        accessService.updateAccess(access);
        return R.ok();
    }

    @RequestMapping(value = "/gradeAccessList")
    public TablePage gradeAccessList(TablePage tablePage){
        Map param = tablePage.getParam();
        PageHelper.startPage(tablePage.getPageNumber(),tablePage.getPageSize());
        GradeAccessReq gradeAccessReq = new GradeAccessReq();
        gradeAccessReq.setAppId(param.containsKey("appId")?param.get("appId").toString():null);
        gradeAccessReq.setAccessId(param.containsKey("accessId")?param.get("accessId").toString():null);
        gradeAccessReq.setAccessId(param.containsKey("gradeId")?param.get("gradeId").toString():null);
        List<GradeAccessRes> gradeAccessRess = appUserGradeAccessService.queryGradeAccess(gradeAccessReq);
        tablePage.setTotal(new PageInfo<>(gradeAccessRess).getTotal());
        tablePage.setRows(gradeAccessRess);
        return tablePage;
    }


}
